package com.durian.controller;

import com.durian.domain.DTO.ObsDefaultListDTO;
import com.durian.domain.DTO.ObsUserAddDTO;
import com.durian.domain.DTO.ObsUserLoginDTO;
import com.durian.domain.entity.ObsUser;
import com.durian.service.IObsUserService;
import com.durian.utils.JwtUtils;
import com.durian.utils.Result;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Positive;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 用户管理
 *
 * @author Durian
 * @since 2023-11-23
 */
@Validated
@RestController
@RequestMapping("book/user")
@RequiredArgsConstructor
public class ObsUserController {

    private final IObsUserService userService;

    /**
     * 获取用户列表
     */
    @PostMapping("list")
    public Result listUser(
            @RequestBody
            ObsDefaultListDTO defaultListDTO
    ) {
        return Result.success(userService.listUser(defaultListDTO));
    }

    /**
     * 获取所有用户(不分页)
     */
    @GetMapping("list")
    public Result listUser() {
        return Result.success(userService.listUser());
    }

    /**
     * 根据用户id获取用户信息
     */
    @PostMapping("get/{userId}")
    public Result getUser(
            @PathVariable
            @Positive(message = "id格式有误")
            Long userId
    ) {
        return Result.success(userService.getUser(userId));
    }

    /**
     * 根据token获取数据
     */
    @GetMapping("get")
    public Result getUser(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        Claims claims = JwtUtils.getClaimsByToken(token);
        String id = claims.getId();

        return Result.success(userService.getUser(Long.parseLong(id)));
    }


    /**
     * 用户登录
     */
    @PostMapping("login")
    public Result login(
            @RequestBody
            ObsUserLoginDTO userLoginDTO
    ) {
        return Result.success(userService.login(userLoginDTO));
    }

    /**
     * 重置密码
     */
    @PostMapping("reset/{userId}")
    public Result restPassword(
            @PathVariable
            @Positive(message = "id格式有误")
            Long userId
    ) {
        userService.resetPassword(userId);
        return Result.success();
    }

    /**
     * 注册用户
     */
    @PostMapping("register")
    public Result userRegistration(
            @Validated
            @RequestBody
            ObsUserAddDTO userAddDTO
    ) {
        userService.userRegistration(userAddDTO);
        return Result.success();
    }

    /**
     * 更新用户信息
     */
    @PutMapping("update")
    public Result updateUser(
            @RequestBody
            ObsUser user
    ) {
        userService.updateUser(user);
        return Result.success();
    }

    /**
     * 校验手机号
     */
    @PostMapping("check/{phone}")
    public Result checkPhone(
            @PathVariable
            @NotBlank(message = "手机号不能为空")
            String phone
    ) {
        userService.checkPhone(phone);
        return Result.success();
    }
}
